package com.ah.shanxi_demo.sync.client;

import com.ah.shanxi_demo.common.enums.SXPortEnum;
import com.ah.shanxi_demo.entity.SxRegister;
import com.ah.shanxi_demo.entity.SxSync;
import com.ah.http.httpsclient.MyHttpsClient;
import com.ah.http.httpsclient.constants.HttpHeaderContentTypeEnum;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Map;

import static com.ah.shanxi_demo.sync.convert.ConvertSyncParam.convert_sync_sync;
import static com.ah.shanxi_demo.sync.convert.ConvertSyncRequest.convert_sync_request_header;
import static com.ah.shanxi_demo.sync.utils.syncResultConvertObject.sync_result_sync;

/**
 * @author zt 2020/8/15
 */
public class SyncClient {
    private static final Logger log = LoggerFactory.getLogger(SyncClient.class);

    /**
     * 部级平台 同步接口 客户端
     *
     * @param sxRegister
     * @return
     */
    public static SxSync sync_client(SxRegister sxRegister) {

        //部级平台进行同步信息
        String sync_serviceAddress = sxRegister.getServiceAddress() + SXPortEnum.STATE_SYNC.getKey();
        Object responseObject = sdk_sync(sxRegister, sync_serviceAddress);

        //解析返回值 返回同步表模型
        SxSync sxSync = sync_result_sync(responseObject);

        return sxSync;
    }

    /**
     * 同步接口_SDK
     *
     * @param param
     * @param sync_serviceAddress
     * @return
     */
    public static Object sdk_sync(SxRegister param, String sync_serviceAddress) {

        //地址
        String url = sync_serviceAddress;

        //参数
        Map<String, Object> paramsMap = convert_sync_sync(param);

        //请求头
        Map<String, String> headerMap = convert_sync_request_header(paramsMap);

        try {
            
            log.info("sdk_sync【url】：" + JSON.toJSONString(url));
            log.info("sdk_sync【paramsMap】：" + JSON.toJSONString(paramsMap));
            log.info("sdk_sync【headerMap】：" + JSON.toJSONString(headerMap));

            Object responseObject = MyHttpsClient.sendPostHttps(url, paramsMap, headerMap, HttpHeaderContentTypeEnum.JSON.getKey());

            log.info("sdk_sync【responseObject】：" + JSON.toJSONString(responseObject));
            return responseObject;
        } catch (Exception e) {
            log.error("sdk_sync【Exception】：" + JSON.toJSONString(e));
        }

        return null;
    }


}
